package com.tangkun.environment.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tangkun.environment.R;
import com.tangkun.environment.entity.personcenter.BindReturn;
import com.tangkun.environment.network.GlobalUrl;
import com.tangkun.environment.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * @author tangkun
 * @ClassName: QiYeGuanLiAdapter
 * @Description: 企业管理列表适配器
 * @date 2016-08-20 上午11:57:36
 */
public class QiYeGuanLiAdapter extends BaseAdapter {

    private Context context;
    private List<BindReturn.DataBean> dataList = new ArrayList<BindReturn.DataBean>();

    public QiYeGuanLiAdapter(Context context, List<BindReturn.DataBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_qiyeguanli, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (dataList != null) {
            //未认证企业 图像做下盾牌 红色未认证 绿色已认证
            if (dataList.get(position).status == 1) {//1，已绑定"
                holder.ivQiyeguanliDunpai.setVisibility(View.VISIBLE);
                holder.ivQiyeguanliDunpai.setImageResource(R.mipmap.ic_qiyeguanli_lusedunpai);
            } else {//0,未绑定
                holder.ivQiyeguanliDunpai.setVisibility(View.VISIBLE);
                holder.ivQiyeguanliDunpai.setImageResource(R.mipmap.ic_qiyeguanli_hongsedunpai);
            }
            //头像
            Glide.with(context)
                    .load(GlobalUrl.IMG_URL + dataList.get(position).img)
                    .bitmapTransform(new CropCircleTransformation(context))
                    .placeholder(R.mipmap.ic_guanlianqiye_shangchuandianzhao)
                    .dontAnimate()
                    .into(holder.civQiyeguanliPhoto);
            //店铺名字
            if (!TextUtils.isEmpty(dataList.get(position).name)) {
                holder.tvQiyeguanliName.setText(dataList.get(position).name);
            } else {
                holder.tvQiyeguanliName.setText("");
            }
            //地址=区县+街道名称  地址区域仅显示企业所属区县和街道办即可
            String address = "";
            //区县
            if (!TextUtils.isEmpty(dataList.get(position).areaname)) {
                address = dataList.get(position).areaname;
            }
            //街道名称
            if (!TextUtils.isEmpty(dataList.get(position).streetname)) {
                if (!TextUtils.isEmpty(address)) {
                    address = address + "," + dataList.get(position).streetname;
                } else {
                    address = dataList.get(position).streetname;
                }
            }
            //社区
            if (!TextUtils.isEmpty(dataList.get(position).communityname)) {
                if (!TextUtils.isEmpty(address)) {
                    address = address + "," + dataList.get(position).communityname;
                } else {
                    address = dataList.get(position).communityname;
                }
            }
            holder.tvQiyeguanliWeizhi.setText(address);
            //百分比 未认证企业，不显示巡查任务进度。
            holder.tvQiyeguanliBaifenbi.setText(dataList.get(position).finishnum + "/" + dataList.get(position).totalnum);
        }
        return convertView;

    }

    /**
     * 自定义接口回调
     */
    private MyClickListener myClickListener;

    public void setMyClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public interface MyClickListener {
        void onMyClick(int position);
    }

    static class ViewHolder {
        @BindView(R.id.civ_qiyeguanli_photo)
        CircleImageView civQiyeguanliPhoto;
        @BindView(R.id.iv_qiyeguanli_dunpai)
        ImageView ivQiyeguanliDunpai;
        @BindView(R.id.tv_qiyeguanli_name)
        TextView tvQiyeguanliName;
        @BindView(R.id.tv_qiyeguanli_weizhi)
        TextView tvQiyeguanliWeizhi;
        @BindView(R.id.tv_qiyeguanli_baifenbi)
        TextView tvQiyeguanliBaifenbi;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
